package com.kingyea.app.market.versionInfo.service.impl;

import com.kingyea.app.market.client.UserServiceClient;
import com.kingyea.app.market.utils.StateCode;
import com.kingyea.app.market.utils.page.PageInfo;
import com.kingyea.app.market.versionInfo.bean.VersionInfo;
import com.kingyea.app.market.versionInfo.dao.VersionInfoMapper;
import com.kingyea.app.market.versionInfo.service.VersionInfoService;
import com.kingyea.mobilepolice.system.Result;
import com.kingyea.mobilepolice.system.exception.BusinessException;
import com.kingyea.mobilepolice.user.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author
 * @version v1.0.0
 * @description VersionInfo 实现类
 * @date Created in 2018-02-11 11:43:16
 */
@Service
@Transactional
public class VersionInfoServiceImpl implements VersionInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(VersionInfoServiceImpl.class);

    @Resource
    private VersionInfoMapper versionInfoMapper;
    @Autowired
    private UserServiceClient userServiceClient;

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @Override
    public VersionInfo getById(String id) {
        return this.versionInfoMapper.getById(id);
    }

    @Override
    public VersionInfo getCurrentVersionByAppCode(String appCode, Integer versionType) {
        return this.versionInfoMapper.getCurrentVersionByAppCode(appCode, versionType);
    }

    /**
     * @Description: 设置为当前版本
     * @param: [id, appCode, versionType]
     * @return: boolean
     * @author: cxc
     * @Date: 2018/6/6
     */
    @Override
    public boolean updateToCurrentById(String id, String appCode, Integer versionType) {
        VersionInfo version = new VersionInfo();
        version.setCurrentVersion(0);
        version.setAppCode(appCode);
        version.setVersionType(versionType);
        versionInfoMapper.updateForCondition(version);

        version = new VersionInfo();
        version.setCurrentVersion(1);
        version.setId(id);
        return versionInfoMapper.updateForCondition(version) == 1;
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public PageInfo<VersionInfo> list(Map<String, Object> version) throws Exception {
        try {
            PageInfo.startPage(version);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return PageInfo.getPageInfo(versionInfoMapper.list(version));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public int deleteById(String id) throws Exception {
        versionInfoMapper.deleteById(id);
        return 1;
    }


    /**
     * 保存
     *
     * @param versionInfo
     * @return
     */
    @Override
    public VersionInfo save(VersionInfo versionInfo) throws Exception {
        if (StringUtils.isEmpty(versionInfo.getId())) {
            versionInfo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        }
        Result<User> currentUserInfo = userServiceClient.getCurrentUserInfo();
        if (null != currentUserInfo && StateCode.OPERATION_SUCCESS.equals(currentUserInfo.getCode())) {
            versionInfo.setCreater(currentUserInfo.getData().getUserId());
        }
        versionInfo.setCreateDate(new Date());
        versionInfoMapper.save(versionInfo);
        return versionInfo;
    }

    /**
     * 更新
     *
     * @param versionInfo
     * @return
     */
    @Override
    public VersionInfo update(VersionInfo versionInfo) {
        versionInfoMapper.update(versionInfo);
        return versionInfo;
    }

    @Override
    public List<VersionInfo> getListByVersionType(String versionType) {
        List<VersionInfo> listByVersionType = null;
        listByVersionType = versionInfoMapper.getListByVersionType(versionType);
        return listByVersionType;
    }

    @Override
    public Object versionList(Map<String, Object> appBaseInfo) throws Exception {
        PageInfo.startPage(appBaseInfo);
        return PageInfo.getPageInfo(versionInfoMapper.versionList(appBaseInfo));
    }

    @Override
    public Object deletesByAppCode(String appCode) throws Exception {
        if (org.apache.commons.lang3.StringUtils.isBlank(appCode)) {
            throw new BusinessException(StateCode.NO_ARGUMENT_ERROR);
        }
        return versionInfoMapper.deletesByAppCode(appCode);
    }


}
